package com.lyb._05_topic;

import com.lyb.utlis.ConnectionUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

// 消息接受者
public class Rev2 {

    // 消费者2的专属队列
    public static final String NAME_QEUEUE2_TOPIC = "name_qeueue2_topic";

    public static void main(String[] args) throws Exception {
        // 通过连接工具创建链接对象
        Connection connection = ConnectionUtil.getConnection();
        // 创建通道
        Channel channel = connection.createChannel();
        // 声明队列
        channel.queueDeclare(NAME_QEUEUE2_TOPIC,true,false,false,null);
        // 绑定到交换机
        channel.queueBind(NAME_QEUEUE2_TOPIC, Send.NAME_EXCHANGE_TOPIC,"user.*");
        // 同时处理一个消息
        channel.basicQos(1);
        // 接收消费的回调
        Consumer consumer = new  DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者的ID："+consumerTag);
                System.out.println("消息的ID："+envelope.getDeliveryTag());
                System.out.println("交换机："+envelope.getExchange());
                System.out.println("队列："+envelope.getRoutingKey());
                System.out.println("收到消息："+new String(body));
                System.out.println("消息消费成功....................................");
                // 手动签收消息
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        };
        // 接收并消费消息                      false手动签收
        channel.basicConsume(NAME_QEUEUE2_TOPIC,false,consumer);
    }
}
